<?php

namespace App\Http\Controllers\Mobile\Index;

use App\Facades\Resources;
use App\Models\Api\ShieldCompany;
use App\Models\Personal\BrowseResume;
use App\Models\XS\ResumeXS;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Style, Index, Cache, DB, Site;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Common\CollPosition;
use App\Models\Common\CollTwoOption;
use App\Models\Personal\Resume;
use App\Models\Personal\ResumeSend;
use App\Models\Personal\PersonalUser;
use App\Models\Personal\ResumeCertificate;
use App\Models\Personal\ResumeEducationExp;
use App\Models\Personal\ResumeExtra;
use App\Models\Personal\ResumeLanguageSkill;
use App\Models\Personal\ResumeOther;
use App\Models\Personal\ResumeProjectExp;
use App\Models\Personal\ResumeSkill;
use App\Models\Personal\ResumeWorkExp;
use App\Models\Personal\ResumeWorks;
use App\Models\Personal\ResumeWorksImg;
use  App\Models\Company\CompanyUser;
use  App\Models\Company\CompanyBasic;
use  App\Models\Company\CompanyJob;
use  App\Models\Company\BuyResume;
use Symfony\Component\VarDumper\Caster\ResourceCaster;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;

class ResumeController extends CommonController{
    /**
     * 简历数组存在不存在
     * @param Request $request
     * @return mixed
     */
    protected function inToArray($value, $array, $key){
        foreach($array as $k => $v){
            if($v[$key] == $value){
                return true;
            }
        }
        return false;
    }

    /**
     * 简历列表
     * @param Request $request
     * @return mixed
     */
    public function personnel(Request $request){
        $ages = [    //年龄数组
            0 => ['value' => '', 'text' => '不限'], 1 => ['value' => 20, 'text' => '16岁以上-20岁以下'],
            2 => ['value' => 25, 'text' => '25岁以下'], 3 => ['value' => 30, 'text' => '30岁以下'],
            4 => ['value' => 35, 'text' => '35岁以下'], 5 => ['value' => 40, 'text' => '40岁以下'],
            5 => ['value' => 60, 'text' => '45岁以上-60岁以下']
        ];
        $this->ages_value = [
            //对应真实年龄
            20 => ['age_above' => 0, 'age_below' => 20], 25 => ['age_above' => 0, 'age_below' => 25],
            30 => ['age_above' => 0, 'age_below' => 30], 35 => ['age_above' => 0, 'age_below' => 35],
            40 => ['age_above' => 0, 'age_below' => 40], 60 => ['age_above' => 45, 'age_below' => 60]
        ];
        //接收传递值
        $input = $request->only(
            'jobarea1', 'jobarea2', 'jobarea3', 'jobarea4', 'jobarea5',//地区
            'work_year', 'age', 'sex', 'education', 'homeaddress', 'census', 'release_date',
            'jobsort1', 'jobsort2', 'jobsort3', 'jobsort4', 'jobsort5',//工作岗位
            'industry1', 'industry2', 'industry3', 'industry4', 'industry5',//行业
            'havedetails', 'chkphoto_open', 'haveworkexp', 'sortdesc',//checkbox
            'keyword', 'keyword_type', 'release_date'//搜索
        );
        //读取地区编码
        $webareacode = Site::get('webareacode');
        //=>返回地区下编码
        $areas = CollArea::getCity($webareacode);
        //工作经验
        $work_year = CollOption::getOption('work_year');
        //年龄
        $work_years = CollOption::getOption('work_year');

        $pageInput = arrayFilter($input);

        //生成地区链接数组
        $checkarray = array($request->jobarea1, $request->jobarea2, $request->jobarea3, $request->jobarea4, $request->jobarea5);
        array_unshift($areas, ['value' => '', 'text' => "不限"]);
        foreach($areas as $k => $v){
            $area[$k]['url'] = urlPagePack($pageInput, ['jobarea1' => $v['value'], 'jobarea2' => null,
                'jobarea3' => null, 'jobarea4' => null, 'jobarea5' => null]);
            $area[$k]['text'] = $v['text'];
            if(in_array($v['value'], $checkarray) and $v['value'] != ''){
                $area[$k]['check'] = 1;
            }else{
                if($v['value'] == '' and $request->jobarea1 == ''){
                    $area[$k]['check'] = 1;
                }else{
                    $area[$k]['check'] = 0;
                }
            }
        }

        foreach($checkarray as $k => $v){
            if($v != null and !$this->inToArray($v, $areas, 'value')){
                $area[] = array(
                    'url' => urlPagePack($pageInput, ['jobarea1' => $v, 'jobarea2' => null,
                        'jobarea3' => null, 'jobarea4' => null, 'jobarea5' => null]),
                    'text' => CollArea::getAreaName($v),
                    'check' => 1,
                );
            }
        }

        $optionsurl['jobarea'] = urlPagePack($pageInput, ['jobarea1' => null, 'jobarea2' => null,
            'jobarea3' => null, 'jobarea4' => null, 'jobarea5' => null]);

        //工作年限
        array_unshift($work_years, ['value' => '', 'text' => "不限"]);
        foreach($work_years as $k => $v){
            $work_year[$k]['url'] = urlPagePack($pageInput, ['work_year' => $v['value']]);
            $work_year[$k]['text'] = $v['text'];
            $work_year[$k]['value'] = $v['value'];
        }

        //年龄
        foreach($ages as $k => $v){
            $age[$k]['url'] = urlPagePack($pageInput, ['age' => $v['value']]);
            $age[$k]['text'] = $v['text'];
            $age[$k]['value'] = $v['value'];
        }

        //性别
        $optionsurl['sex'] = urlPagePack($pageInput, ['sex' => null]);
        //学历
        $optionsurl['education'] = urlPagePack($pageInput, ['education' => null]);
        //现所在地
        $optionsurl['homeaddress'] = urlPagePack($pageInput, ['homeaddress' => null]);
        //户藉
        $optionsurl['census'] = urlPagePack($pageInput, ['census' => null]);
        //显示详细信息
        $optionsurl['havedetails'] = urlPagePack($pageInput, ['havedetails' => null]);
        //有照片
        $optionsurl['chkphoto_open'] = urlPagePack($pageInput, ['chkphoto_open' => null]);
        //有工作经验
        $optionsurl['haveworkexp'] = urlPagePack($pageInput, ['haveworkexp' => null]);
        //智能排序
        $optionsurl['sortdesc'] = urlPagePack($pageInput, ['sortdesc' => null]);
        //工作经验
        $optionsurl['work_year'] = urlPagePack($pageInput, ['work_year' => null]);
        //搜索关键字
        $optionsurl['keyword'] = urlPagePack($pageInput, ['keyword' => null]);
        //分页
        $optionsurl['page'] = urlPagePack($pageInput, ['page' => null]);
        //压入中文到数组
        //地区
        $cnoption['jobarea1'] = CollArea::getAreaName($request->jobarea1);
        $cnoption['jobarea2'] = CollArea::getAreaName($request->jobarea2);
        $cnoption['jobarea3'] = CollArea::getAreaName($request->jobarea3);
        $cnoption['jobarea4'] = CollArea::getAreaName($request->jobarea4);
        $cnoption['jobarea5'] = CollArea::getAreaName($request->jobarea5);
        $cnoption['jobarea'] = trim($cnoption['jobarea1'] . '+' . $cnoption['jobarea2'] . '+' . $cnoption['jobarea3'] .
            '+' . $cnoption['jobarea4'] . '+' . $cnoption['jobarea5'], '+');

        //行业
        $jobsort1 = ($request->jobsort1) ? CollPosition::getPosName($request->jobsort1) : '';
        $jobsort2 = ($request->jobsort2) ? CollPosition::getPosName($request->jobsort2) : '';
        $jobsort3 = ($request->jobsort3) ? CollPosition::getPosName($request->jobsort3) : '';
        $jobsort4 = ($request->jobsort4) ? CollPosition::getPosName($request->jobsort4) : '';
        $jobsort5 = ($request->jobsort5) ? CollPosition::getPosName($request->jobsort5) : '';
        $cninput['jobsort'] = trim($jobsort1 . '+' . $jobsort2 . '+'
            . $jobsort3 . '+' . $jobsort4 . '+' . $jobsort5, '+');
        $optionsurl['jobsort'] = urlPagePack($pageInput, ['jobsort1' => null, 'jobsort2' => null,
            'jobsort3' => null, 'jobsort4' => null, 'jobsort5' => null]);

        $cnoption['jobsort1'] = $jobsort1;
        $cnoption['jobsort2'] = $jobsort2;
        $cnoption['jobsort3'] = $jobsort3;
        $cnoption['jobsort4'] = $jobsort4;
        $cnoption['jobsort5'] = $jobsort5;

        $cnoption['jobsort'] = $cninput['jobsort'];

        //工作年限
        $cnoption['work_year'] = CollOption::getOptionName($request->work_year, 'work_year');

        //年龄
        if(!empty($request->age)){
            foreach($age as $k => $v){
                if($v['value'] == $request->age){
                    $cnoption['age'] = $v['text'];
                }
            }
        }else{
            $cnoption['age'] = '';
        }

        //性别
        if($request->sex == 1){
            $sexvalue = "男";
        }elseif($request->sex == 2){
            $sexvalue = "女";
        }else{
            $sexvalue = '';
        }
        $cnoption['sex'] = $sexvalue;
        //学历
        $cnoption['education'] = CollOption::getOptionName($request->education, 'education');
        //现所在地
        $cnoption['homeaddress'] = CollArea::getAreaName($request->homeaddress);
        //户藉
        $cnoption['census'] = CollArea::getAreaName($request->census);

        //详细信息、有照片、有工作经验、分页、智能排序
        $cnoption['havedetails'] = '';
        $cnoption['chkphoto_open'] = '';
        $cnoption['haveworkexp'] = '';
        $cnoption['page'] = '';
        $cnoption['sortdesc'] = '';
        $cnoption['keyword_type'] = '';
        $cnoption['keyword'] = msubstr($input['keyword'], 0, 10);
        $cnoption['page'] = '';

        //生成去除条件链接
        $del_list = array();
        foreach($pageInput as $key => $value){
            $ikey = substr($key, 0, -1);
            if($ikey != 'jobarea' and $ikey != 'jobsort'){
                if($cnoption[$key]){
                    $del_list[] = array('text' => $cnoption[$key], 'url' => urlPagePack($pageInput, [$key => null]));
                }
            }else{
                if(substr($key, -1, 1) == 1){
                    $key1 = $ikey . '1';
                    $key2 = $ikey . '2';
                    $key3 = $ikey . '3';
                    $key4 = $ikey . '4';
                    $key5 = $ikey . '5';
                    $del_list[] = array('text' => msubstr($cnoption[$ikey], 0, 10), 'url' => urlPagePack($pageInput,
                        [$key1 => null, $key2 => null, $key3 => null, $key4 => null, $key5 => null]));
                }
            }
        }

        //联合查询
        if(\Site::get('xsOpen') && (!empty($request->keyword || !empty($request->jobsort1)))){
            $resumeinfo = $this->xunSearch($request);
            if($resumeinfo->total() < 1) $resumeinfo = $this->sqlSearch($request);
        }else{
            $resumeinfo = $this->sqlSearch($request);
        }
        $page = $resumeinfo->appends($pageInput)->links();//分页链接

        $edu_exp = [];
        $work_exp = [];

        //基本信息查询
        foreach($resumeinfo as $v){
            //学历
            if(empty($v->education)){
                $v->education = '';
            }else{
                $v->education = CollOption::getOptionName($v->education, 'education');
            }
            //年龄
            if(empty($v->birthday)){
                $v->birthday = '';
            }else{
                $v->birthday = birthday($v->birthday) . '岁';    //年龄
            }
            //工作经验
            if(empty($v->work_year)){
                $v->work_year = '';
            }else{
                $v->work_year = CollOption::getOptionName($v->work_year, 'work_year');
            }
            //工作地区  1
            if(empty($v->jobarea1)){
                $v->jobarea1 = '';
            }else{
                $v->jobarea1 = CollArea::getAreaName($v->jobarea1);
            }
            if(empty($v->jobarea2)){
                $v->jobarea2 = '';
            }else{
                $v->jobarea2 = CollArea::getAreaName($v->jobarea2);
            }
            if(empty($v->jobarea3)){
                $v->jobarea3 = '';
            }else{
                $v->jobarea3 = CollArea::getAreaName($v->jobarea3);
            }
            if(empty($v->jobarea4)){
                $v->jobarea4 = '';
            }else{
                $v->jobarea4 = CollArea::getAreaName($v->jobarea4);
            }
            if(empty($v->jobarea5)){
                $v->jobarea5 = '';
            }else{
                $v->jobarea5 = CollArea::getAreaName($v->jobarea5);
            }
            $v->jobarea = $v->jobarea1 . ' ' . $v->jobarea2 . ' ' . $v->jobarea3 . ' ' . $v->jobarea4 . ' ' . $v->jobarea5;
        }

        //设置标题
        $area_name = !empty($request->jobarea1) ? $cnoption['jobarea1'] : Site::get('area_name');
        $keyword = (strlen($request->keyword) > 0) ? $pageInput['keyword'] : '';
        $personal_search = (strlen($keyword) > 0) ? $area_name . $keyword . '招聘|' : '';

        return Style::view(
            'mobile.index.personnel', compact('pageInput', 'resumeinfo', 'input', 'area', 'work_year',
                'age', 'optionsurl', 'del_list', 'cninput', 'work_exp', 'edu_exp', 'page', 'lastpage', 'personal_search')
        );
    }

    /** 迅搜搜索
     * @param $request
     * @return \Illuminate\Pagination\LengthAwarePaginator
     */
    private function xunSearch($request){
        //查询条件
        $q = $request->all();

        //黑名单id
        $user = Auth::guard('company');
        if($user->check()){
            $shield = ShieldCompany::where('company_uid', $user->user()->uid)->pluck('resume_id');
            if(count($shield) > 0){
                $q['shield'] = $shield->toArray();
            }
        }

        $jobarea = [];//工作地点
        $jobsort = [];//所属岗位
        for($i = 1; $i <= 5; $i++){
            $area = $request->input('jobarea' . $i);
            $sort = $request->input('jobsort' . $i);
            if(!empty($area)){
                $jobarea[] = $area;
            }
            if(!empty($sort)){
                $jobsort[] = $sort;
            }
        }
        $q['jobarea'] = $jobarea;
        $q['jobsort'] = $jobsort;

        //更新日期
        $q['updatetime'] = $request->release_date;

        //户藉
        if(!empty($request->census) && $request->census != 'other'){
            $q['census'] = $request->census;
        }

        //年龄
        if(!empty($request->age)){
            if(array_key_exists($request->age, $this->ages_value)){
                $age = [];
                $age['age_above'] = $this->ages_value[$request->age]['age_above'];
                $age['age_below'] = $this->ages_value[$request->age]['age_below'];
                $q['age'] = $age;
            }
        }

        //关键字
        if(!empty($request->keyword)){
            if($request->keyword_type == 2){
                $q['name'] = $request->keyword;
            }elseif($request->keyword_type == 3){
                $q['intentionjobs'] = $request->keyword;
            }else{
                $q['keyword'] = $request->keyword;
            }
        }

        //创建迅搜对象
        $xs = new ResumeXS();
        //查询页码
        $page = $request->input('page', 1);
        if($page > 200) $page = 200;
        //搜索(参数:搜索条件,第几页,一页个数,获取总数,是否按照时间排序)
        $arr = $xs->searchResume($q, $page, 20, true);
        //分页并返回
        return $this->pageInfo($arr['data'], $arr['total'], $request);
    }

    /** sql搜索
     * @param $request
     * @return mixed
     */
    private function sqlSearch($request){
        //处理数据查询
        $resumeinfo = DB::table('resume as t1')
            // ->join('resume_extra as t2', 't1.id', '=', 't2.resume_id')
            //获取字段
            ->select(
                'id', 'uid', 'name', 'sex', 'birthday', 'education', 'homeaddress',
                'work_year', 'intentionjobs', 'jobarea1', 'jobarea2', 'jobarea3',
                'jobarea4', 'jobarea5', 'updatetime', 'avatar', 'resume_status'
            )
            //工作地点
            ->where(function($query) use ($request){
                $city = [];
                for($i = 1; $i <= 5; $i++){
                    if(!empty($request->input('jobarea' . $i))){
                        $query->orWhere('jobarea1', '=', $request->input('jobarea' . $i))
                            ->orWhere('jobarea2', '=', $request->input('jobarea' . $i))
                            ->orWhere('jobarea3', '=', $request->input('jobarea' . $i))
                            ->orWhere('jobarea4', '=', $request->input('jobarea' . $i))
                            ->orWhere('jobarea5', '=', $request->input('jobarea' . $i));
                        $city[] = substr($request->input('jobarea' . $i), 0, 4) . '00';
                    }else{
                        break;
                    }
                }
                if(count($city) > 0){
                    $city = array_unique($city);
                    foreach($city as $v){
                        $query->orWhere('jobarea1', '=', $v)
                            ->orWhere('jobarea2', '=', $v)
                            ->orWhere('jobarea3', '=', $v)
                            ->orWhere('jobarea4', '=', $v)
                            ->orWhere('jobarea5', '=', $v);
                    }
                }
            })
            //工作经验 当前所选工作经验以上
            ->where(function($query) use ($request){
                if(!empty($request->work_year)) $query->where('work_year', '>=', $request->work_year);
            })
            //年龄要求 当前所选大于或者等于年龄以上的
            ->where(function($query) use ($request){
                if(!empty($request->age)){
                    $age_above = $this->ages_value[$request->age]['age_above'];
                    $age_below = $this->ages_value[$request->age]['age_below'];

                    if($age_above == 0){
                        $greater = date('Y-m-d');
                    }else{
                        //当前用工最低年龄
                        $date = date('Y-m-d');
                        $date = date_create($date);
                        $days = $age_above * 365 + intval($age_above / 4);
                        date_sub($date, date_interval_create_from_date_string("$days days"));
                        $greater = date_format($date, "Y-m-d");
                    }
                    //当前用工最高年龄
                    if($age_below > 0){
                        $date = date('Y-m-d');
                        $date = date_create($date);
                        $days = $age_below * 365 + intval($age_below / 4);
                        date_sub($date, date_interval_create_from_date_string("$days days"));
                        $lesstime = date_format($date, "Y-m-d");
                    }
                    $query->whereBetween('birthday', [$lesstime, $greater]);
                }
            })
            //所属岗位
            ->where(function($query) use ($request){
                for($i = 1; $i <= 5; $i++){
                    if(!empty($request->input('jobsort' . $i)))
                        $query->orWhere('jobsort1', '=', $request->input('jobsort' . $i))
                            ->orWhere('jobsort2', '=', $request->input('jobsort' . $i))
                            ->orWhere('jobsort3', '=', $request->input('jobsort' . $i))
                            ->orWhere('jobsort4', '=', $request->input('jobsort' . $i))
                            ->orWhere('jobsort5', '=', $request->input('jobsort' . $i));
                    else break;
                }
            })
            //性别要求
            ->where(function($query) use ($request){
                if(!empty($request->sex)) $query->where('sex', '=', $request->sex);
            })
            //学历要求
            ->where(function($query) use ($request){
                if(!empty($request->education)) $query->where('education', '=', $request->education);
            })
            //现居住地
            ->where(function($query) use ($request){
                if(!empty($request->homeaddress)) $query->where('homeaddress', '=', $request->homeaddress);
            })
            //搜索
            ->where(function($query) use ($request){
                if(!empty($request->keyword)){
                    if($request->keyword_type == 2){
                        $query->where('name', 'like', '%' . $request->keyword . '%');
                    }elseif($request->keyword_type == 3){
                        $query->where('intentionjobs', 'like', '%' . $request->keyword . '%');
                    }else{
                        //全文搜索时？？？？
                        $query->where('name', 'like', '%' . $request->keyword . '%')
                            ->orWhere('intentionjobs', 'like', '%' . $request->keyword . '%');
                    }
                }
            })
            //发布日期
            ->where(function($query) use ($request){
                if(!empty($request->release_date)){
                    //获取选中的多少时间内
                    $ctime = date("Y-m-d H:i:s", strtotime("-" . $request->release_date . "day"));
                    $query->where('t1.updatetime', '>=', $ctime);
                }
            })
            //照片情况
            ->where(function($query) use ($request){
                if($request->chkphoto_open == 1){//0代表显示
                    $query->where('chkphoto_open', 0)
                        ->where('avatar', '<>', '');
                }
            })
            //简历状态是1--公开
            ->where('resume_status', '=', '1')
            //户藉
            ->where(function($query) use ($request){
                if(!empty($request->census)) $query->where('census', '=', $request->census);
            });

        $count = $resumeinfo->count();

        //数据排序方式
        switch($request->sortdesc){
            case 1:
                $resumeinfo = $resumeinfo->orderBy('updatetime', 'desc')->orderBy('id', 'desc');
                break;
            default:
                $resumeinfo = $resumeinfo->orderBy('updatetime', 'desc')->orderBy('id', 'desc');
        }
        $count = $resumeinfo->count();
        $resumeinfo = $resumeinfo->skip($this->getSkip($request->page))->take(20)->get();
        //分页并返回
        return $this->pageInfo($resumeinfo, $count, $request);
    }

    /**
     * 分页信息
     */
    protected function pageInfo($data, $total, $request){
        $perPage = 20;//每页显示20条
        $current_page = $request->input('page', 1);
        $total = $total > 4000 ? 4000 : $total;
        $company_job = new LengthAwarePaginator($data, $total, $perPage, $current_page, [
            'path' => Paginator::resolveCurrentPath(), //注释2
            'pageName' => 'page',
        ]);
        return $company_job;
    }

    /**
     * 取得分页启始数
     */
    protected function getSkip($page){
        if(empty($page)) $page = 1;
        if($page > 200) $page = 200;
        return ($page - 1) * 20;
    }


    /**
     * 简历详情
     * @param $id
     * @param Request $request
     * @return mixed
     */
    public function personalInfo($id, Request $request){
        $id = idDecryption($id);
        //用户ID
        $suid = $request->input('suid');
        //读取基本信息
        $resume = Resume::where('id', $id)->first();//resume_status

        //判断能浏览简历的参数 管理员可以，自己可以，简历状态为1
        $user = auth()->guard('company')->user();//企业登录
        if(auth()->guard('company')->check()){
            $CompanyBasic = CompanyBasic::select("id")->where("uid", $user->uid)->orderBy("updatetime", "desc")->first();//是否给企业投递过简历
            //企业不存在时
            if(empty($CompanyBasic->id)){
                return redirect("mobile/company/register_basic");
            }
            if(ResumeSend::where("company_uid", $user->uid)->where("com_id", $CompanyBasic->id)->where("personal_uid", $resume->uid)->where("resume_id", $id)->count()){
                $companyLookResume = 1;
            }else{
                $companyLookResume = 0;
            }
        }else{
            $companyLookResume = 0;
        }

        if(auth()->guard('personal')->check()){//用户登录
            $personalUid = auth()->guard('personal')->user()->uid;
        }else{
            $personalUid = '';
        }
        //判断条件是否有权浏览
        if($personalUid == $resume->uid || auth()->guard('admin')->check() || $resume->resume_status == 1 || $companyLookResume == 1){
            if($resume){
                //头像
                if(!empty($resume->avatar) && $resume->chkphoto_open == 0){
                    $resume->avatar = asset('upload/personalavatar/' . $resume->avatar);
                }else if($resume->sex == '1'){
                    $resume->avatar = asset('default/images/mobile/layout/photo01.png');
                }else if($resume->sex == '2'){
                    $resume->avatar = asset('default/images/mobile/layout/photo02.png');
                }
                //学历
                $resume->education = CollOption::getOptionName($resume->education, 'education');
                //工作时长
                $resume->work_year = CollOption::getOptionName($resume->work_year, 'work_year');
                //工作状态
                $resume->job_status = CollOption::getOptionName($resume->job_status, 'job_status');
                //性别
                $resume->sex = ($resume->sex == '1') ? '男' : '女';
                //婚姻
                $resume->marital = empty($resume->marital) ? '保密' : CollOption::getOptionName($resume->marital, 'marital');
                //户藉
                $resume->census = empty($resume->census) ? '未填写' : CollArea::getAreaName($resume->census);
                //户藉
                $resume->homeaddress = empty($resume->homeaddress) ? '未填写' : CollArea::getAreaName($resume->homeaddress);
                //身高
                $resume->height = empty($resume->height) ? '未填写' : $resume->height . 'CM';
                //工作地区
                if(empty($resume->jobarea1)){
                    $resume->jobarea1 = '';
                }else{
                    $resume->jobarea1 = CollArea::getAreaName($resume->jobarea1);
                }
                if(empty($resume->jobarea2)){
                    $resume->jobarea2 = '';
                }else{
                    $resume->jobarea2 = CollArea::getAreaName($resume->jobarea2);
                }
                if(empty($resume->jobarea3)){
                    $resume->jobarea3 = '';
                }else{
                    $resume->jobarea3 = CollArea::getAreaName($resume->jobarea3);
                }
                if(empty($resume->jobarea4)){
                    $resume->jobarea4 = '';
                }else{
                    $resume->jobarea4 = CollArea::getAreaName($resume->jobarea4);
                }
                if(empty($resume->jobarea5)){
                    $resume->jobarea5 = '';
                }else{
                    $resume->jobarea5 = CollArea::getAreaName($resume->jobarea5);
                }
                $resume->jobarea = $resume->jobarea1 . ' ' . $resume->jobarea2 . ' ' . $resume->jobarea3 . ' ' . $resume->jobarea4 . ' ' . $resume->jobarea5;
                $resume->jobarea = trim($resume->jobarea);
                $resume->jobarea = empty($resume->jobarea) ? '未填写' : $resume->jobarea;
                //工作岗位
                CollPosition::getPosName($resume->jobsort1);
                if(empty($resume->jobsort1)){
                    $resume->jobsort1 = '';
                }else{
                    $resume->jobsort1 = CollPosition::getPosName($resume->jobsort1);
                }
                if(empty($resume->jobsort2)){
                    $resume->jobsort2 = '';
                }else{
                    $resume->jobsort2 = CollPosition::getPosName($resume->jobsort2);
                }
                if(empty($resume->jobsort3)){
                    $resume->jobsort3 = '';
                }else{
                    $resume->jobsort3 = CollPosition::getPosName($resume->jobsort3);
                }
                if(empty($resume->jobsort4)){
                    $resume->jobsort4 = '';
                }else{
                    $resume->jobsort4 = CollPosition::getPosName($resume->jobsort4);
                }
                if(empty($resume->jobsort5)){
                    $resume->jobsort5 = '';
                }else{
                    $resume->jobsort5 = CollPosition::getPosName($resume->jobsort5);
                }
                $resume->jobsort = $resume->jobsort1 . ' ' . $resume->jobsort2 . ' ' . $resume->jobsort3 . ' ' . $resume->jobsort4 . ' ' . $resume->jobsort5;
                $resume->jobsort = trim($resume->jobsort);
                $resume->jobsort = empty($resume->jobsort) ? '未填写' : $resume->jobsort;
                //期望岗位
                $resume->intentionjobs  = empty($resume->intentionjobs ) ? '未填写' : $resume->intentionjobs;
                //工资
                $resume->expectedsalary = empty($resume->expectedsalary) ? '面议' : CollOption::getOptionName($resume->expectedsalary, 'expectedsalary') . '元/月';

                //获取邮箱
                if(!empty($resume->email)){
                    $list = explode('@', $resume->email);
                    $resume->stremail = substr_replace($list[0], '****', 2, 4) . (!empty($list[1]) ? '@' . $list[1] : '');
                }else{
                    $resume->stremail = '';
                }
                //获取手机
                if(!empty($resume->mobile)){
//                $sNum = str_repeat('*', strlen($resume->mobile) - 5);
//                $resume->strmobile = str_replace(substr($resume->mobile, 3, -2), $sNum, $resume->mobile);
                    $resume->strmobile = preg_replace('/(\d{3})(\d{4})(\d{4})/i', '$1****$3', $resume->mobile);
                }else{
                    $resume->strmobile = '';
                }

                //工作经验
                $ResumeWorkExp = ResumeWorkExp::select('starttime', 'endtime', 'company', 'post')->
                where('resume_id', $resume->id)->where('uid', $resume->uid)->get();
                foreach($ResumeWorkExp as $v){
                    $v->starttime = substr($v->starttime, 0, 7);
                    $v->endtime = ($v->endtime == '0000-00-00') ? '至今' : substr($v->endtime, 0, 7);
                }
                //教育经历
                $ResumeEducationExp = ResumeEducationExp::select('starttime', 'endtime', 'school', 'speciality')->
                where('resume_id', $resume->id)->where('edu_type', 1)->where('uid', $resume->uid)->get();
                foreach($ResumeEducationExp as $v){
                    $v->starttime = substr($v->starttime, 0, 7);
                    $v->endtime = ($v->endtime == '0000-00-00') ? '至今' : substr($v->endtime, 0, 7);
                }
                //培训经历
                $trainExp = ResumeEducationExp::select('starttime', 'endtime', 'school', 'speciality')->
                where('resume_id', $resume->id)->where('edu_type', 2)->where('uid', $resume->uid)->get();
                foreach($trainExp as $v){
                    $v->starttime = substr($v->starttime, 0, 7);
                    $v->endtime = ($v->endtime == '0000-00-00') ? '至今' : substr($v->endtime, 0, 7);
                }
                //项目经验
                $ResumeProjectExp = ResumeProjectExp::select('project_name', 'starttime', 'endtime', 'post')
                    ->where('resume_id', $resume->id)->where('uid', $resume->uid)->get();
                foreach($ResumeProjectExp as $v){
                    $v->starttime = substr($v->starttime, 0, 7);
                    $v->endtime = ($v->endtime == '0000-00-00') ? '至今' : substr($v->endtime, 0, 7);
                }
                //语言能力
                $ResumeLanguageSkill = ResumeLanguageSkill::select('language', 'degree')
                    ->where('resume_id', $resume->id)->where('uid', $resume->uid)->get();
                foreach($ResumeLanguageSkill as $v){
                    $v->language = CollOption::getOptionName($v->language, 'language');
                    $v->degree = CollOption::getOptionName($v->degree, 'degree');
                }
                //技能专长
                $ResumeSkill = ResumeSkill::select('skillname', 'degree')->where('resume_id', $resume->id)
                    ->where('uid', $resume->uid)->get();
                foreach($ResumeSkill as $v){
                    $v->degree = CollOption::getOptionName($v->degree, 'degree');
                }
                //证书
                $ResumeCertificate = ResumeCertificate::select('certificate_name', 'gettime')
                    ->where('resume_id', $resume->id)->where('uid', $resume->uid)->get();
                foreach($ResumeCertificate as $v){
                    $v->gettime = substr($v->gettime, 0, 4);
                }
                //其它信息
                $ResumeOther = ResumeOther::select('title', 'content')->where('resume_id', $resume->id)
                    ->where('uid', $resume->uid)->get();
                //我的作品
                $works = ResumeWorks::select('id', 'works_name')
                    ->whereHas('worksBelongImg', function($query){
                        $query->orderBy('id', 'desc');
                    })
                    ->with(["worksBelongImg" => function($query){
                        $query->select('id', 'works_id', 'imgurl', 'originalimg')
                            ->orderBy("resume_works_img.id", 'desc');
                    }])
                    ->where('uid', $resume->uid)->get();

                $worksType = ['gif', 'jpg', 'jpeg', 'bmp', 'png'];
                foreach($works as $v){
                    $suffix = substr(strrchr($v->worksBelongImg->imgurl, '.'), 1);
                    if(in_array($suffix, $worksType)){
                        $imgurl = 'upload/personalworks/' . $v->worksBelongImg->imgurl;
                    }else{
                        switch($suffix){
                            case 'doc' :
                                $imgurl = 'default/images/personal/doc.jpg';
                                break;
                            case 'docx' :
                                $imgurl = 'default/images/personal/doc.jpg';
                                break;
                            case 'ppt' :
                                $imgurl = 'default/images/personal/ppt.jpg';
                                break;
                            case 'pptx' :
                                $imgurl = 'default/images/personal/ppt.jpg';
                                break;
                            case 'xls' :
                                $imgurl = 'default/images/personal/xls.jpg';
                                break;
                            case 'xlsx' :
                                $imgurl = 'default/images/personal/xls.jpg';
                                break;
                            case 'pdf' :
                                $imgurl = 'default/images/personal/pdf.jpg';
                                break;
                            case 'txt' :
                                $imgurl = 'default/images/personal/txt.jpg';
                                break;
                            default :
                                $imgurl = '未知';
                                break;
                        }
                    }
                    $v->worksBelongImg->imgurl = $imgurl;
                }

                //企业登录
                $user = auth()->guard('company')->user();
                if($user){
                    $lookStatus = $this->lookResumeStatus($user->uid, $id);
                    $look_button = "button1";//查看联系方式button
                    $interview_button = "button2"; //邀请面试

//                    $ResumeSend = ResumeSend::where('company_uid',$user->uid)->where('resume_id',$id)->orderBy('created_at','desc')
//                        ->where('status',0)->get();
//                    if(count($ResumeSend) > 0){
//                        ResumeSend::where('company_uid',$user->uid)->where('resume_id',$id)->update(['status'=>1]);
//                    }

                    //是不是已经投递 并且该企业必须是会员才能查看
                    $ResumeSend = ResumeSend::select('created_at')->where("resume_id", $id)->where('company_uid', $user->uid)->first();
                    if(!empty($ResumeSend)){
                        if($user->viptime > date('Y-m-d H:i:s')){
                            $lookStatus = 2;
                        }
                    }

                    //添加企业浏览简历记录
                    if(!empty($CompanyBasic)){
                        $browseResume = BrowseResume::where("com_id", $CompanyBasic->id)->where("company_uid", $user->uid)->where("resume_id", $id)->first();
                        if(empty($browseResume)){
                            $addBrowseResume = [];
                            $addBrowseResume["company_uid"] = $user->uid;
                            $addBrowseResume["com_id"] = $CompanyBasic->id;
                            $addBrowseResume["personal_uid"] = $resume->uid;
                            $addBrowseResume["resume_id"] = $id;
                            BrowseResume::create($addBrowseResume);
                        }
                    }
                    //查询该简历是否给企业投过
                    $ResumeSend = ResumeSend::where('company_uid', $user->uid)->where('resume_id', $id)->orderBy('created_at', 'desc')
                        ->where('status', 0)->get();
                    if(count($ResumeSend) > 0){
                        ResumeSend::where('company_uid', $user->uid)->where('resume_id', $id)->update(['status' => 1]);
                    }
                }else{
                    $lookStatus = 0;
                    $look_button = "companylogin";
                    $interview_button = "companylogin2";
                }

                // 预览简历时操作
                $personalUser = auth()->guard('personal')->user();
                if(!empty($personalUser)){
                    $resumeinfo = Resume::where("id", $id)->where('uid', $personalUser->uid)->first();
                    if(!empty($resumeinfo)){
                        $lookStatus = 3;
                    }
                }
                //找人才标题
                $jobsortall = trim($resume->intentionjobs . "," . $resume->jobsort1 . ',' . $resume->jobsort2 . ',' . $resume->jobsort3 . ',' . $resume->jobsort4 . ',' . $resume->jobsort5, ',');
                $find_job = $resume->jobarea1 . '找' . $jobsortall . '工作,' . $resume->id . '的个人简历';

                return Style::view('mobile.index.personalInfo', compact('resume', 'ResumeWorkExp', 'ResumeEducationExp',
                    'ResumeProjectExp', 'ResumeLanguageSkill', 'ResumeSkill', 'ResumeCertificate', 'ResumeOther', 'trainExp',
                    'lookStatus', 'look_button', 'interview_button', 'works', 'personalUid', 'find_job'));
            }else{//没有简历id处理
                $exception = "简历已关闭";
                return Style::view('errors.mobile_404', compact('exception'));
            }
        }else{//没有权限处理
            $exception = "简历已关闭";
            return Style::view('errors.mobile_404', compact('exception'));
        }
    }

    /**
     * 返回简历是否被查看
     * @param $uid
     * @param $resumeId
     * @return int
     */
    public function lookResumeStatus($uid, $resumeId){
        if(!empty($uid)){
            $BuyResume = BuyResume::where('company_uid', $uid)->where('resume_id', $resumeId)->first();
            if($BuyResume){
                $lookStatus = 1;//简历被查看过
            }else{
                $lookStatus = 0;//简历未被查看过
            }
        }else{
            $lookStatus = 0;//未登录
        }
        return $lookStatus;
    }


    /**
     * 时间差
     * @param $time
     * @return int
     */
    public function timediff($time){
        $timezone = Config::get('app.timezone', 'PRC');
        date_default_timezone_set($timezone);
        $zero1 = strtotime(date("Y-m-d H:i:s", time()));
        $zero2 = strtotime($time);
        $time_c = $zero2 - $zero1;
        return $time_c;
    }

}